﻿using DevExpress.XtraReports.UI;
using DevicesView.Entity;
using DevicesView.Logic;
using System.Collections.Generic;
using BCL.Tool.Extend;
using DevicesView.Logic.App;
using System.Drawing;
using XiaonuanPMS.Logic;
using XiaonuanPMS.Entity;
using XiaonuanPMS.Entity.View;
using System;
using System.Globalization;

namespace Devices.Print
{
    public partial class LabReportA4 : XtraReport
    {

        private Devices.Result result;

        private string OrgName { get; set; }

        public string PrintClassName { get; set; }

        private TestingsLogic logic;

        private RPResult rPBreedDatas;//HFY添加
        private List<RPDataDic> rpPetTypes;//HFY添加

        //LabResultVO entity;

        public LabReportA4(List<Devices.Result> _result)
        {
            InitializeComponent();
            result = _result?[0];
            //添加文字水印
            //PrintWaterMark.SetTextWatermark(this.Report.RootReport, "公司机密请勿泄露");
            //添加图片水印
            // PrintWaterMark.SetPictureWatermark(this.Report.RootReport);
            this.PaperKind = System.Drawing.Printing.PaperKind.A4;
        }

        private void LabReportA4_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            logic = new XiaonuanPMS.Logic.TestingsLogic();
            //HqLabResultLogic logic = new HqLabResultLogic();
            //HqLabResult entity = logic.GetLabResult(orgId, regid, reportId);
            if (result != null)
            {
                Devices.Command cmd = result.CMD;
                //labNo.Text = orgId.ToString() + reportId.ToString();
                
                // 获取宠物信息
                RPTestPetInfo pet = logic.GetTestPet(cmd.PetId);
                if (pet == null)
                {
                    txtOldMedRecordNo.Text = ""; // 病历号
                }
                else {
                    txtOldMedRecordNo.Text = pet.oldMedRecordNo; // 病历号
                }

                labNo.Text = cmd.Id;
                txtOrgName.Text = AppResource.CurrentResource?.Hospital?.Name;
                txtCustomerName.Text = cmd.Customer;//.cus_name;
                txtGender.Text = cmd.Gender == Devices.GenderType.Female ?"雌" : "雄" ;
                //txtKindOf.Text = cmd.KindOf.ToString();// labReport.pet_kindof_txt;
                // txtPetRecordNum.Text = cmd.PetId;// "";// labReport.record_number;

                txtPetName.Text = cmd.PetName;

                //HFY添加，判断宠物品种
                rPBreedDatas = logic.GetPetBreed();
                if (pet.KindOF == "cat")
                {
                    txtKindOf.Text = "猫";
                    for (int i = 0; i <= rPBreedDatas.catBreed.Count; i++)
                    {
                        if (int.Parse(rPBreedDatas.catBreed[i].codeValue) == int.Parse(pet.petBreed))
                        {
                            txtVarity.Text = rPBreedDatas.catBreed[i].codeName;
                            break;
                        }
                    }
                }
                if (pet.KindOF == "dog")
                {
                    txtKindOf.Text = "犬";
                    for (int i = 0; i <= rPBreedDatas.dogBreed.Count; i++)
                    {
                        if (int.Parse(rPBreedDatas.dogBreed[i].codeValue) == int.Parse(pet.petBreed))
                        {
                            txtVarity.Text = rPBreedDatas.dogBreed[i].codeName;
                            break;
                        }
                    }
                }
                if (pet.KindOF == "other")
                {
                    txtKindOf.Text = "异宠";
                    for (int i = 0; i <= rPBreedDatas.otherBreed.Count; i++)
                    {
                        if (int.Parse(rPBreedDatas.otherBreed[i].codeValue) == int.Parse(pet.petBreed))
                        {
                            txtVarity.Text = rPBreedDatas.otherBreed[i].codeName;
                            break;
                        }
                    }
                }
                //rpPetTypes.FindIndex(o => o.codeValue == pet.KindOF);
                rpPetTypes = logic.GetPetType();
                

                txtWeight.Text = string.Format("{0:F2}", cmd.Weight);

                txtPhysician.Text = cmd.Doctor;// labReport.exec_physician_name;
                txtPrescriptionDate.Text = result.Date.ToString();// Convert.ToDateTime(labReport.reporting_time).ToString("yyyy-MM-dd HH:mm");

                // HFY添加
                //petInfo.ageYear
                //if (int.TryParse(cmd.Age, out age))

                DateTime pet_birth = Convert.ToDateTime(pet.birthdate);
                DateTime getnow = DateTime.Now;
                calculateTime calTime = new calculateTime();
                calTime.GetAgeByBirthday(pet_birth, getnow, out int year, out int month, out int day);

                /*TimeSpan date = DateTime.Now - pet_birth;
                int ageYear = (date.Days / 365);*/
                int ageYear = year;
                if (ageYear > 0)
                {
                    txtAge.Text = ageYear + "岁" + month + "个月" + day + "天";                  
                }
                else
                {
                    txtAge.Text = month + "个月"+ day + "天";
                    /*txtAge.Text += day + "天";*/
                }
                /*if (int.TryParse(ageYear, out age))
                {
                    if (age > 0)
                        txtAge.Text = age + "岁";
                    txtAge.Text += pet.birthdate + "月";
                }*/

                // 结果分析
                if (result.Remarks != null && result.Remarks.Count > 0)
                {
                    resultText.Text = result.Remarks[0].ToString();
                }
                else
                {
                    resultText.Text = "无";
                }               

                lblReportName.Text = cmd.Name;
                xrTextRemark.Visible = false;


                int imageheight = 67;
                if (AppResource.CurrentResource.Hospital?.Logo != null)
                {
                    Image image = Image.FromStream(new System.IO.MemoryStream(AppResource.CurrentResource.Hospital?.Logo));
                    if (image.Height > imageheight)
                    {
                        double intWidth = ((double)imageheight / (double)image.Height) * (double)image.Width;
                        xrPictureBox3.Image = new Bitmap(image, new Size((int)intWidth, imageheight));
                    }

                }

                //List<HqImgs> imgs = entity.imgs;
                //if (imgs != null && imgs.Count > 0)
                //{
                //    xrSubreportImg.ReportSource = new LabReportVirusA4Img(imgs);
                //}

                //List<HqLabReportDetails> results = entity.labvirus_details;

                xrSubreportData.ReportSource = new LabReportA4Data(result);
            }
            //TODO:系统1019开关设置
            //if (Warmsoft.ClientUtilities.Utilities.GetPrivilige(1019))
            if (true)
            {
                //Org orgInfo = ((AppResource)BCLApplication.Resource).OrgInfo;
                //SysClientManagementLogic logic = new SysClientManagementLogic();
                //SysClientInfo clientInfo = logic.GetSysClientInfo(orgInfo.systemId ?? 0, orgInfo.brandId);

                //try
                //{
                //    xrPictureBox3.Image = ImageUtil.DownloadImage(clientInfo.configs.reportLogo);
                //}
                //catch (Exception ex)
                //{

                //}
            }
        }

    }
}
